package farmer.update.building;

import farmer.game.Data;
import pathfinding.grid.GridPath;

import java.util.ArrayList;

import farmer.render.unit.Transporter;
import farmer.core.Building;
import farmer.core.BuildingRessource;

public abstract class City extends Building{
	
	protected ArrayList<Transporter> transporterList = new ArrayList<Transporter>();
	private ArrayList<Transporter> transporterAtBaseList = new ArrayList<Transporter>();
	private final int totalTransporter = 20;
	
	protected int wood = 0;
	protected int food = 0;
	protected int rock = 0;
	
	protected ArrayList<BuildingRessource> connectionList = new ArrayList<BuildingRessource>();
	private ArrayList<BuildingRessource> buildingList = new ArrayList<BuildingRessource>();
	private ArrayList<BuildingRessource> buildingSelectedList = new ArrayList<BuildingRessource>();
	private ArrayList<BuildingRessource> buildingNoPathList = new ArrayList<BuildingRessource>(); 
	
	public City(int x, int y) {
		super(x, y);
		size = 5;
	}
	
	public void placeBuilding(){
		for(int i=0; i<totalTransporter; i++){
			Transporter transporter = new Transporter(posX+2, posY+2, this);
			transporterList.add(transporter);
			map.addUnit(transporter);
			transporterAtBaseList.add(transporter);
		}
	}
	
	public void setConnection(BuildingRessource building){
		if(!(buildingList.contains(building)) && !(buildingSelectedList.contains(building))){
			buildingList.add(building);
			connectionList.add(building);
		}
	}
	
	public void removeConnection(BuildingRessource building){
		buildingList.remove(building);
		connectionList.remove(building);
		buildingSelectedList.remove(building);
	}
	
	public void addFreeTransporter(Transporter transporter){
		transporterAtBaseList.add(transporter);
		wood += transporter.getWood();
		food += transporter.getFood();
		rock += transporter.getRock();
		transporter.resetRessource();
		Data.food += food;
		Data.wood += wood;
		Data.rock += rock;
		food = 0;
		wood = 0;
		rock = 0;
	}
	
	public void isReturningToBase(Transporter transporter){
		BuildingRessource building = transporter.getBuilding();
		if(buildingSelectedList.contains(building)){
			buildingSelectedList.remove(building);
		}
	}
	
	public void destroy(){
		for(int i=0; i<transporterList.size(); i++){
			map.removeUnit(transporterList.get(i));
		}
	}
	
	public void update(int deltaTime){
		for(int i=0; i<transporterAtBaseList.size(); i++){
			//Find a tree that is not selected
			//Find the path to that tree
			//Set the path of the unit
			Transporter transporter = transporterAtBaseList.get(i);
			BuildingRessource building = getFirstBuildingWithRessource();
			if(building != null){
				GridPath path = map.getPath(transporter, building);
				if(path != null){
					buildingSelectedList.add(building);
					transporter.setPath(path);
					transporter.setBuilding(building);
					transporterAtBaseList.remove(i);
					i--;
				}else{
					System.out.println("Buildign move to no pathList");
					buildingNoPathList.add(building);
				}
			}
		}
		for(int i=0; i<buildingNoPathList.size(); i++){
			BuildingRessource building = buildingNoPathList.get(i);
			buildingList.add(building);
		}
		buildingNoPathList.clear();
	}
	
	private BuildingRessource getFirstBuildingWithRessource(){
		BuildingRessource result = null;
		for(int i=0; i<buildingList.size(); i++){
			if(buildingList.get(i).hasRessource() && !buildingSelectedList.contains(buildingList.get(i))){
				result = buildingList.remove(i);
				buildingList.add(result);
			}
		}
		return result;
	}

}
